<template>
  <div>
    <el-dialog
      title="新增流程"
      :visible="visible"
      width="40%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-form"
        label-position="right"
        size="mini"
        style="width:80%"
        v-loading="loading"
      >
        <el-form-item
          label="名称"
          prop="name"
          :label-width="defaultLabelWidth"
        >
          <el-input size="mini" type="text" v-model="form.name"></el-input>
        </el-form-item>
         <el-form-item
          label="描述"
          prop="name"
          :label-width="defaultLabelWidth"
        >
          <el-input size="mini" type="textarea" v-model="form.des"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      id: 1
    }
  },
  watch: {
    value: {
      handler() {
        //this.load();
      },
      deep: true
    },
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    }
  },
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {
        name: ""
      },
      rules:{
         name:[
          { required: true, message: "请输入", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      if (value) this.loadData(value);
    },
    hide() {
      this.visible = false;
      this.form = {
        name: ""
      };
    },
    loadData(value) {
      this.loading = true;
      Object.keys(value).forEach(key => {
        this.form[key] = value[key];
      });
      this.loading = false;
    },
    commit() {
      const _this = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$store.dispatch("workflow/createWorkFlow", this.form).then(res => {
            this.$emit("ok-click", res);
            this.hide();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style></style>
